package com.lanchetech.im.controller;

import com.lanchetech.bean.request.UserOnlineReq;
import com.lanchetech.bean.request.WsSendToUserReq;
import com.lanchetech.bean.response.BaseResp;
import com.lanchetech.bean.response.ResultData;
import com.lanchetech.bean.response.UserOnlineResp;
import com.lanchetech.common.enums.ResultCode;
import com.lanchetech.im.config.WebSocketServer;
import com.lanchetech.im.service.ImService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

@Api(value = "websocket请求", tags = "websocket请求")
@RequestMapping("/api/websocket")
@RestController
public class WebSocketController {

    @Autowired
    ImService imService;

    @ApiOperation(value = "向用户发送WS消息")
    @PostMapping("/sendToUser")
    public BaseResp sendToUser(@RequestBody WsSendToUserReq req) {
        try {
            return WebSocketServer.sendToUser(req.getMsgUserId(), req.getImMessageType(), req.getMessage());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new BaseResp(ResultCode.FAIL);
    }

    @ApiOperation(value = "获取用户是否在线")
    @PostMapping("/isUserOnline")
    public ResultData<List<UserOnlineResp>> isUserOnline(@RequestBody UserOnlineReq req) {
        return WebSocketServer.isUserOnline(req);
    }

}
